



# 对长文本进行分段
def chunk_text_by_paragraph(text, min_chunk_size=200):
    """
    根据段落对文本进行分块，并确保每个块至少包含一定数量的字符。
    
    :param text: 要分块的完整文本
    :param min_chunk_size: 每个块最小的字符数
    :return: 分块后的文本列表
    """
    chunks = []
    current_chunk = ""
    
    paragraphs = text.split('\n')
    for paragraph in paragraphs:
        if len(current_chunk) + len(paragraph) < min_chunk_size:
            current_chunk += paragraph + "\n"
        else:
            chunks.append(current_chunk.strip())
            current_chunk = paragraph + "\n"
    
    if current_chunk:  # 添加最后一个块
        chunks.append(current_chunk.strip())
    
    return chunks


def split_into_segments(text, max_length=600, overlap=100):
    """
    将文本按照指定的最大长度和重叠长度分割成多个段落。
    
    :param text: 要分割的原始文本
    :param max_length: 每个段落的最大token数
    :param overlap: 相邻段落之间的重叠token数
    :return: 分割后的段落列表
    """
    
    segments = []
    start = 0
    text_length = len(text)

    while start < text_length:
        end = min(start + max_length, text_length)
        segment = text[start:end]
        segments.append(segment)
        
        # 移动窗口，保留 overlap 长度的重叠
        start += max_length - overlap

    return segments